package com.bdth.bdthiss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthiss.vo.Pagination;
import com.bdth.bdthiss.entity.User;
import com.bdth.bdthiss.mapper.UserMapper;
import com.bdth.bdthiss.service.UserService;
import com.bdth.bdthiss.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LGP
 * @since 2018-11-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Pagination<User> pageLists(Pagination<User> pagination, Map<String, Object> param) {
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        if((String) param.get("username")!=null) {
            entityWrapper.like("username", (String) param.get("username"));
        }
        if((String) param.get("nickname")!=null){
            entityWrapper.like("nickname", (String) param.get("nickname"));
        }
        if((String) param.get("email")!=null) {
            entityWrapper.like("email", (String) param.get("email"));
        }
        if((String) param.get("departids")!=null) {
            entityWrapper.gt("departid",0);
            entityWrapper.in("departid", (String) param.get("departids"));
        }
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<User> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());
        List<User> items = userMapper.selectPage(page,entityWrapper);
        Integer total = userMapper.selectCount(entityWrapper);
        pagination.setItems(items);
        pagination.setTotal(total);
        return pagination;
    }

    @Override
    public User doLogin(String username, String password) {
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("username",username);
        entityWrapper.eq("password", MD5Utils.getMD5String(password));
        List<User> list = userMapper.selectList(entityWrapper);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean changePwd(String username, String password) {
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("username",username);
        User user = new User();
        user.setPassword(MD5Utils.getMD5String(password));
        Integer result = userMapper.update(user,entityWrapper);
        if(result>0)
            return true;
        return false;
    }
}
